const{
    userInsert
}=require("../../utils/insertAndDelete");
Page({
    data:{
        islogin:'1',
        loginbtnstate:'true',
        registeriphone:'',
        registerpassword:'',
        combackpassword:'',
        strloginUser:'',
        strloginpassword:'',
        switchChecked:'',
    },
    onLoad:function(options){
        var that=this
        that.setData({
            strloginUser:wx.getStorageSync('iphone1'),
            strloginpassword:wx.getStorageSync('password1'),
            switchChecked:wx.getStorageSync('checkValue1'),
            loginbtnstate:wx.getStorageSync('wxlogin1')
        })
    },
    onlogin:function(e){  //登录实现
        var that=this
        console.log(that.data)
        if(that.data.switchChecked==true){
            wx.setStorageSync('iphone1', that.data.strloginUser);
            wx.setStorageSync('password1', that.data.strloginpassword);
            wx.setStorageSync('checkedValue1', that.data.switchChecked);
            wx.setStorageSync('wxlogin1', false);
        }
        else if(that.data.switchChecked==false){
            wx.setStorageSync('iphone1', '');
            wx.setStorageSync('password1', '');
            wx.setStorageSync('checkedValue1','');
            wx.setStorageSync('wxlogin1', true);
        }
        wx.setStorageSync('iphone', that.data.strloginUser);
            wx.setStorageSync('password', that.data.strloginpassword);
        // var m=wx.getStorageSync('userjob2')  //从缓存中拿到user数据
        wx.request({  //从数据库拿到整个user表，在一个一个地对比
          url: 'http://122.112.245.197:5000/api/user',
          success:(res)=>{
              console.log(res.data)
              var k=false; //判断是否遍历完user表
              for(var i=0;i<res.data.length;i++){
                if((that.data.strloginUser==res.data[i].userID)&&(that.data.strloginpassword==res.data[i].userpassword))
                {
                    wx.showToast({
                        title: '登录成功',
                    })
                    wx.switchTab({
                        url: '../index/index',
                      })
                      k=true;
                }
              }
              if(k==false) {
                wx.showToast({
                    title: '密码错误',
                  })
              }

          }
        })
        
        // if(that.data.strloginUser==m.iphone){  //判断账号密码是否正确
        //     if(that.data.strloginpassword==m.password){
        //         wx.showToast({
        //           title: '登录成功',
        //         })
        //         wx.switchTab({
        //           url: '../index/index',
        //         })
        //     }
        //     else{
        //         wx.showToast({
        //           title: '密码错误',
        //         })
        //     }
        // }
        // else{
        //     wx.showToast({
        //       title: '手机号未注册',
        //     })
        // }
    },
    bindswitchchange:function(e){//是否记住密码
        var that=this
        var m=e.detail.value
        if(m==true){
            that.setData({
                switchChecked:m,
            })
        }
        else if(m==false){
            that.setData({
                switchChecked:m,
            })
        }
    },
  
    compass:function(e){//判断按钮是否可以工作
        var that=this
        var m=e.detail.value
        if(m!=''&&(m.length==11)){
            that.setData({
                combackpassword:m,
                loginbtnstate:false
            })
        }
        else{
            that.setData({
                loginbtnstate:true
            })
        }
    },
    combackpassword:function(e){  //找回密码
        var that=this
        var value=wx.getStorageSync('userjob2')
        if(that.data.combackpassword==value.iphone){
            wx.setClipboardData({
              data: 'value.password',
              success(res){
                  wx.getClipboardData({
                    success: (option) => {
                        console.log(option.data+"密码")
                        that.setData({
                            islogin:1
                        })
                        wx.showToast({
                          title: '密码已复制',
                          duration:2000
                        })
                    },
                  })
              }
            })
        }
        else{
            wx.showToast({
              title: '手机号未注册',
            })
        }
    },
    inputiphone1:function(e){//登录手机号判断
        var that=this
        var m=e.detail.value
        console.log(e.detail.value)
        if(m!=''&&(m.length==11)){ //手机号是否是11位
            that.setData({
                strloginUser:m
            })
        
        if(that.data.strloginpassword!=''){ //密码是否为空
            that.setData({
                loginbtnstate:false
            })
        }
        else{
            that.setData({
                loginbtnstate:true
            })
        }
       
        } 
        else{
            that.setData({
                loginbtnstate:true
            })
            }
       
    },
    password1:function(e){  //登录密码判断
        var that=this
        var m=e.detail.value
        if(m!='')
        {
            that.setData({
                strloginpassword:m
            })
            if(that.data.strloginUser!=''&&(that.data.strloginUser.length==11)&&(that.data.strloginpassword!='')){
                that.setData({
                    loginbtnstate:false
                })
            }
        }
        else{
            that.setData({
                loginbtnstate:true
            })
        }
    },
    inputiphone2:function(e){//注册手机号判断
        var that=this
        var m=e.detail.value
        console.log(e.detail.value)
        if(m!=''&&(m.length==11)){
            that.setData({
                registeriphone:m
            })
        
        if(that.data.registerpassword!=''){
            that.setData({
                loginbtnstate:false
            })
        }
        else{
            that.setData({
                loginbtnstate:true
            })
        }
       
        } 
        else{
            that.setData({
                loginbtnstate:true
            })
            }
       
    },
    password2:function(e){  //注册密码判断
        var that=this
        var m=e.detail.value
        if(m!='')
        {
            that.setData({
                registerpassword:m
            })
            if(that.data.registeriphone!=''&&(that.data.registeriphone.length==11)&&(that.data.registerpassword!='')){
                that.setData({
                    loginbtnstate:false
                })
            }
        }
        else{
            that.setData({
                loginbtnstate:true
            })
        }
    },
    formsubmit:function(e){  //注册
        console.log(e)
        var obj={}  //写在缓存的账号
        obj.iphone=e.detail.value.registerinputiphone
        obj.password=e.detail.value.registerinputpassword
        wx.setStorageSync('userjob2',obj)
        var data={    //写到数据库的账号
            userID:obj.iphone,
            userpassword:obj.password
        }
        userInsert({
            data,
            success:(res)=>{
                console.log(res)
            }
        })
        wx.showToast({
            title:'注册成功'
        })
        this.setData({
            islogin:1
        })
    },
    register:function(e){
        this.setData({
            islogin:3
        })
    },
    forget:function(e){
        this.setData({
            islogin:2
        })
    },
    denglu:function(e){
        this.setData({
            islogin:1
        })
    }
})